package com.packagetracker.adapters;

import java.util.LinkedList;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.packagetracker.R;
import com.packagetracker.entities.Carrier;
import com.packagetracker.entities.TrackingNumber;
import com.packagetracker.views.TrackerNumbers;
import com.packagetracker.views.ViewHolder;

public class PackageListAdapter extends BaseExpandableListAdapter {
	
	private Context _context;
	private LayoutInflater _inflater;
	private LinkedList<TrackingNumber> _numbers;
	
	public PackageListAdapter(final Context context, final LinkedList<TrackingNumber> numbers) {
		this._context = context;
		this._inflater = LayoutInflater.from(context);
		this._numbers = numbers;
	}
	
	@Override
	public View getGroupView(final int position, final boolean isExpanded, View convertView, final ViewGroup parent) {
		try {
			ViewHolder holder;
			
			if (convertView == null) {
				convertView = this._inflater.inflate(R.layout.numbers_view_item, null);
				
				holder = new ViewHolder();
				holder.setNumber((TextView)convertView.findViewById(R.id.manage_item_text));
				holder.setIcon((ImageView)convertView.findViewById(R.id.manage_item_icon));
			}
			else {
				holder = (ViewHolder)convertView.getTag();
			}
			
			final TrackingNumber number = this._numbers.get(position);
			
			final Carrier carrier = new Carrier(number.getCarrier());
			
			holder.getNumber().setText(number.getNumber());
			holder.getIcon().setImageBitmap(BitmapFactory.decodeResource(this._context.getResources(), carrier.getIcon()));
			
			convertView.setTag(holder);
			
			return convertView;
		}
		catch (Exception e) {}
		
		return(null);
	}
	
	@Override
	public View getChildView(final int groupPosition, final int childPosition, final boolean isLastChild, View convertView, final ViewGroup parent) {
		try {
			ViewHolder holder;
			
			if (convertView == null) {
				convertView = this._inflater.inflate(R.layout.numbers_view_item, null);
				
				holder = new ViewHolder();
				holder.setNumber((TextView)convertView.findViewById(R.id.manage_item_text));
				holder.setIcon((ImageView)convertView.findViewById(R.id.manage_item_icon));
			}
			else {
				holder = (ViewHolder)convertView.getTag();
			}
			
			holder.getNumber().setText(this.getChild(groupPosition, childPosition).toString());
			holder.getIcon().setImageBitmap(BitmapFactory.decodeResource(this._context.getResources(), R.drawable.icon_world));
			
			convertView.setTag(holder);
			
			return(convertView);
		}
		catch (Exception e) {}
		
		return null;
	}
	
	@Override
	public long getGroupId(final int groupPosition) {
		return groupPosition;
	}
	
	@Override
	public Object getGroup(final int position) {
		return this._numbers.get(position);
	}
	
	@Override
	public int getGroupCount() {
		return this._numbers.size();
	}
	
	@Override
	public long getChildId(final int groupPosition, final int childPosition) {
		return childPosition;
	}
	
	@Override
	public Object getChild(final int groupPosition, final int childPosition) {
		return ((TrackerNumbers)this._context).getMenuOptions()[childPosition];
	}
	
	@Override
	public int getChildrenCount(final int position) {
		return ((TrackerNumbers)this._context).getMenuOptions().length;
	}
	
	@Override
	public boolean isChildSelectable(final int groupPosition, final int childPosition) {
		return true;
	}
	
	@Override
	public boolean hasStableIds() {
		return true;
	}
	
}